package com.sky.service.impl;

import com.sky.annotation.LogOperation;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
	@Autowired
	private AddressBookMapper addressBookMapper;

	@Override
	public List<AddressBook> list(AddressBook addressBook) {
		return addressBookMapper.list(addressBook);
	}

	@Override
	public void save(AddressBook addressBook) {
		addressBookMapper.save(addressBook);
	}

	@Override
	public AddressBook getById(Long id) {
		return addressBookMapper.getById(id);
	}

	@Override
	public void update(AddressBook addressBook) {
		addressBookMapper.update(addressBook);
	}

	@LogOperation
	@Override
	public void updateDefault(AddressBook addressBook) {
		//1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
		addressBook.setIsDefault(0);
		addressBook.setUserId(BaseContext.getCurrentId());
		addressBookMapper.updateIsDefaultByUserId(addressBook);

		//2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
		addressBook.setIsDefault(1);
		addressBookMapper.updateDefault(addressBook);

	}

	@Override
	public AddressBook queryDefault() {
		Long userId = BaseContext.getCurrentId();
		return addressBookMapper.getDefault(userId);
	}
}
